﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Net;

namespace chatblog.usercate.dialog
{
    public partial class newRoom : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(chRoom));
            AjaxPro.Utility.RegisterTypeForAjax(typeof(newRoom));
            cbuser.dealcookie();

            if (!IsPostBack)
            {
                string errorMsg = string.Empty;
                if (Request["torecid"] != null && AliApi.appUserId != string.Empty)
                {
                    cbuser user = cbuser.Find("recid="+Request["torecid"]+"");
                    if (user.Count > 0)
                    {
                        string user_to = user.cbuserid.Value;
                        string user_from = AliApi.appUserId;
                        cbChatRoom chatRoom = cbChatRoom.Find("chat_to='" + user_to + "' and chat_from='" + user_from + "'");
                        if (chatRoom.Count > 0)     //已经打开了此聊天室
                        {
                            errorMsg = "此聊天窗口在其他地方已经打开！";
                        }
                        else //打开聊天室
                        {
                            DateTime openTime = DateTime.Now;
                            string hostName = HttpContext.Current.Request.UserHostName ;// Dns.GetHostName();
                            //IPAddress[] ipAddressList = Dns.GetHostAddresses(hostName);
                            string ip = HttpContext.Current.Request.UserHostAddress;// ipAddressList[0].ToString();
                            string chatAddress = "ip=" + ip + ";hostname=" + hostName;
                            chatRoom.New();
                            chatRoom.chat_to.Value = user_to;
                            chatRoom.chat_from.Value = user_from;
                            chatRoom.chatRoomId.Value = chatRoom.GetRecID().ToString();
                            chatRoom.chatOpenTime.Value = openTime;
                            chatRoom.chatAddress.Value = chatAddress;
                            chatRoom.Insert();

                            cbChatRoom_Struct chatRoom_Struct = new cbChatRoom_Struct();
                            chatRoom_Struct.user_to = user_to;
                            chatRoom_Struct.user_from = user_from;
                            chatRoom_Struct.openTime = openTime;
                            chatRoom_Struct.chatAddress = chatAddress;
                            Util.ChatRoomStructList.Add(chatRoom_Struct);
                        }  
                    }
                    else
                    {
                        errorMsg = "未找到您的聊天对象！";
                    }
                }
                else 
                {
                    errorMsg = "您还未登录或者聊天对象不存在！";
                }
            }          
        }


        private string GetCurrentNewRoomStatus(string user_to_recid)
        {
            string result = string.Empty;
            if (user_to_recid != string.Empty && AliApi.appUserId != string.Empty)
            {
                cbuser user = cbuser.Find("recid=" + user_to_recid + "");
                if (user.Count > 0)
                {
                    string user_to = user.cbuserid.Value;
                    string user_from = AliApi.appUserId;
                    cbChatRoom chatRoom = cbChatRoom.Find("chat_to='" + user_to + "' and chat_from='" + user_from + "'");
                    if (chatRoom.Count > 0)     //已经打开了此聊天室
                    {
                        cbChatRoom_Struct chatRoom_struct = Util.GetChatRoom(user_to, user_from);
                        if (chatRoom_struct.user_to == string.Empty || chatRoom_struct.user_from == string.Empty)
                        {
                            result = "此聊天窗口在其他地方已经打开！";
                        }
                        else
                        {
                            if (chatRoom_struct.chatAddress != chatRoom.chatAddress.Value || chatRoom.chatOpenTime.Value.ToString() != chatRoom_struct.openTime.ToString())
                            {  
                                result = "此聊天窗口在其他地方已经打开！";
                            }
                        }
                    }
                    else
                    {
                        result = "您的聊天窗口出现异常！";
                    }
                }
                else
                {
                    result = "未找到您的聊天对象";
                }
            }
            else
            {
                result = "您还未登录或者聊天对象不存在！";
            }
            return result;
        }


        /// <summary>
        /// 移除数据库中保存的聊天室对象信息
        /// </summary>
        /// <param name="user_to_recid"></param>
        [AjaxPro.AjaxMethod]
        public void  RemoveCurrentChatRoom(string user_to_recid)
        {
            string result = string.Empty;
            if (user_to_recid != string.Empty && AliApi.appUserId != string.Empty && GetCurrentNewRoomStatus(user_to_recid )==string.Empty )
            {
                cbuser user = cbuser.Find("recid=" + user_to_recid + "");
                if (user.Count > 0)
                {
                    string user_to = user.cbuserid.Value;
                    string user_from = AliApi.appUserId;
                    cbChatRoom chatRoom = cbChatRoom.Find("chat_to='" + user_to + "' and chat_from='" + user_from + "'");
                    if (chatRoom.Count > 0)     //已经打开了此聊天室
                    {
                        chatRoom.Delete("chat_to='" + user_to + "' and chat_from='" + user_from + "'");
                        Util.RemoveChatRoom(user_to, user_from);
                    }                    
                }                
            }              
        }
    }   
}
